package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.YqdaFilePath;
import cn.yuanqiao.archive.appManager.mapper.YqdaFilePathMapper;
import cn.yuanqiao.archive.appManager.service.IYqdaFilePathSerive;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class YqdaFilePathSeriveImpl implements IYqdaFilePathSerive {

    @Autowired
    private YqdaFilePathMapper yqdaFilePathMapper;
    @Override
    @Transactional
    public int saveYqdaFilePath(YqdaFilePath yqdaFilePath) {
        if(yqdaFilePath == null || yqdaFilePath.getCategoryId() == null){
            throw new ServiceException("参数错误");
        }
        YqdaFilePath update = new YqdaFilePath();
        update.setState(1);
        update.setCategoryId(yqdaFilePath.getCategoryId());
        yqdaFilePathMapper.updateYqdaFilePath(update);

        int r= 0;
        if(StringUtils.isNotEmpty(yqdaFilePath.getFilePath())){
            yqdaFilePath.setCreateTime(new Date());
            yqdaFilePath.setState(0);
            yqdaFilePath.setUserName(SecurityUtils.getUsername());
            yqdaFilePath.setTenantCode(SecurityUtils.getTenantCode());
            r = yqdaFilePathMapper.insertYqdaFilePath(yqdaFilePath);
        }
        return r;
    }

    @Override
    public YqdaFilePath selectYqdaFilePath(YqdaFilePath yqdaFilePath) {
        return yqdaFilePathMapper.selectYqdaFilePath(yqdaFilePath);
    }

    @Override
    public List<YqdaFilePath> selectYqdaFilePathList(YqdaFilePath yqdaFilePath) {
        return yqdaFilePathMapper.selectYqdaFilePathList(yqdaFilePath);
    }

    @Override
    public int updateYqdaFilePath(YqdaFilePath yqdaFilePath) {
        return yqdaFilePathMapper.updateYqdaFilePath(yqdaFilePath);
    }

    @Override
    public String getFilePath(Long categoryId) {
        YqdaFilePath filePath = new YqdaFilePath();
        filePath.setState(0);
        filePath.setCategoryId(categoryId);
        filePath.setTenantCode(SecurityUtils.getTenantCode());
        YqdaFilePath yqdaFilePath = yqdaFilePathMapper.selectYqdaFilePath(filePath);
        return yqdaFilePath == null ? null:yqdaFilePath.getFilePath();
    }
}
